

<!DOCTYPE html>
<html class="writer-html5" lang="en" >
<head>
  <meta charset="utf-8" />
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  
  <title>mindspore.nn.Cell &mdash; MindSpore master documentation</title>
  

  
  <link rel="stylesheet" href="../../_static/css/theme.css" type="text/css" />
  <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" />

  
  

  
  

  

  
  <!--[if lt IE 9]>
    <script src="../../_static/js/html5shiv.min.js"></script>
  <![endif]-->
  
    
      <script type="text/javascript" id="documentation_options" data-url_root="../../" src="../../_static/documentation_options.js"></script>
        <script src="../../_static/jquery.js"></script>
        <script src="../../_static/underscore.js"></script>
        <script src="../../_static/doctools.js"></script>
        <script src="../../_static/language_data.js"></script>
        <script async="async" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/latest.js?config=TeX-AMS-MML_HTMLorMML"></script>
    
    <script type="text/javascript" src="../../_static/js/theme.js"></script>

    
    <link rel="index" title="Index" href="../../genindex.html" />
    <link rel="search" title="Search" href="../../search.html" />
    <link rel="next" title="mindspore.nn.GraphCell" href="mindspore.nn.GraphCell.html" />
    <link rel="prev" title="mindspore.nn" href="../mindspore.nn.html" /> 
</head>

<body class="wy-body-for-nav">

   
  <div class="wy-grid-for-nav">
    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search" >
          

          
            <a href="../../index.html" class="icon icon-home"> MindSpore
          

          
          </a>

          
            
            
          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        
        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
            
            
              
            
            
              <p class="caption"><span class="caption-text">MindSpore Python API</span></p>
<ul class="current">
<li class="toctree-l1"><a class="reference internal" href="../mindspore.html">mindspore</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.common.initializer.html">mindspore.common.initializer</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.communication.html">mindspore.communication</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.compression.html">mindspore.compression</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.context.html">mindspore.context</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.dataset.html">mindspore.dataset</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.dataset.audio.html">mindspore.dataset.audio</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.dataset.config.html">mindspore.dataset.config</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.dataset.text.html">mindspore.dataset.text</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.dataset.transforms.html">mindspore.dataset.transforms</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.dataset.vision.html">mindspore.dataset.vision</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.mindrecord.html">mindspore.mindrecord</a></li>
<li class="toctree-l1 current"><a class="reference internal" href="../mindspore.nn.html">mindspore.nn</a><ul class="current">
<li class="toctree-l2 current"><a class="reference internal" href="../mindspore.nn.html#id1">基本构成单元</a><ul class="current">
<li class="toctree-l3 current"><a class="current reference internal" href="#">mindspore.nn.Cell</a></li>
<li class="toctree-l3"><a class="reference internal" href="mindspore.nn.GraphCell.html">mindspore.nn.GraphCell</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="../mindspore.nn.html#id2">容器</a></li>
<li class="toctree-l2"><a class="reference internal" href="../mindspore.nn.html#id3">卷积层</a></li>
<li class="toctree-l2"><a class="reference internal" href="../mindspore.nn.html#id4">梯度</a></li>
<li class="toctree-l2"><a class="reference internal" href="../mindspore.nn.html#id5">循环层</a></li>
<li class="toctree-l2"><a class="reference internal" href="../mindspore.nn.html#id6">稀疏层</a></li>
<li class="toctree-l2"><a class="reference internal" href="../mindspore.nn.html#id7">非线性激活函数</a></li>
<li class="toctree-l2"><a class="reference internal" href="../mindspore.nn.html#id8">工具</a></li>
<li class="toctree-l2"><a class="reference internal" href="../mindspore.nn.html#id9">图像</a></li>
<li class="toctree-l2"><a class="reference internal" href="../mindspore.nn.html#id10">归一化层</a></li>
<li class="toctree-l2"><a class="reference internal" href="../mindspore.nn.html#id11">池化层</a></li>
<li class="toctree-l2"><a class="reference internal" href="../mindspore.nn.html#id12">量化</a></li>
<li class="toctree-l2"><a class="reference internal" href="../mindspore.nn.html#id13">损失函数</a></li>
<li class="toctree-l2"><a class="reference internal" href="../mindspore.nn.html#id14">优化器</a></li>
<li class="toctree-l2"><a class="reference internal" href="../mindspore.nn.html#wrapper">Wrapper</a></li>
<li class="toctree-l2"><a class="reference internal" href="../mindspore.nn.html#id15">数学运算</a></li>
<li class="toctree-l2"><a class="reference internal" href="../mindspore.nn.html#id16">评估指标</a></li>
<li class="toctree-l2"><a class="reference internal" href="../mindspore.nn.html#id17">动态学习率</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.nn.probability.html">mindspore.nn.probability</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.nn.transformer.html">mindspore.nn.transformer</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.numpy.html">mindspore.numpy</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.ops.html">mindspore.ops</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.parallel.html">mindspore.parallel</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.parallel.nn.html">mindspore.parallel.nn</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.profiler.html">mindspore.profiler</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.scipy.html">mindspore.scipy</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.train.html">mindspore.train</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.boost.html">mindspore.boost</a></li>
</ul>
<p class="caption"><span class="caption-text">MindSpore C++ API</span></p>
<ul>
<li class="toctree-l1"><a class="reference external" href="https://www.mindspore.cn/lite/api/zh-CN/master/api_cpp/mindspore.html">MindSpore Lite↗</a></li>
</ul>

            
          
        </div>
        
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" aria-label="top navigation">
        
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../../index.html">MindSpore</a>
        
      </nav>


      <div class="wy-nav-content">
        
        <div class="rst-content">
        
          

















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="../../index.html" class="icon icon-home"></a> &raquo;</li>
        
          <li><a href="../mindspore.nn.html">mindspore.nn</a> &raquo;</li>
        
      <li>mindspore.nn.Cell</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
          
            <a href="../../_sources/api_python/nn/mindspore.nn.Cell.rst.txt" rel="nofollow"> View page source</a>
          
        
      </li>
    
  </ul>

  
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <div class="section" id="mindspore-nn-cell">
<h1>mindspore.nn.Cell<a class="headerlink" href="#mindspore-nn-cell" title="Permalink to this headline">¶</a></h1>
<dl class="class">
<dt id="mindspore.nn.Cell">
<em class="property">class </em><code class="sig-prename descclassname">mindspore.nn.</code><code class="sig-name descname">Cell</code><span class="sig-paren">(</span><em class="sig-param">auto_prefix=True</em>, <em class="sig-param">flags=None</em><span class="sig-paren">)</span><a class="headerlink" href="#mindspore.nn.Cell" title="Permalink to this definition">¶</a></dt>
<dd><p>MindSpore中神经网络的基本构成单元。模型或神经网络层应当继承该基类。</p>
<p><cite>mindspore.nn</cite> 中神经网络层也是Cell的子类，如 <a class="reference internal" href="mindspore.nn.Conv2d.html#mindspore.nn.Conv2d" title="mindspore.nn.Conv2d"><code class="xref py py-class docutils literal notranslate"><span class="pre">mindspore.nn.Conv2d</span></code></a>、<a class="reference internal" href="mindspore.nn.ReLU.html#mindspore.nn.ReLU" title="mindspore.nn.ReLU"><code class="xref py py-class docutils literal notranslate"><span class="pre">mindspore.nn.ReLU</span></code></a>、 <code class="xref py py-class docutils literal notranslate"><span class="pre">mindspore.nn.BatchNorm</span></code> 等。Cell在GRAPH_MODE(静态图模式)下将编译为一张计算图，在PYNATIVE_MODE(动态图模式)下作为神经网络的基础模块。</p>
<p><strong>参数：</strong></p>
<ul class="simple">
<li><p><strong>auto_prefix</strong> (bool) – 是否自动为Cell及其子Cell生成NameSpace。<cite>auto_prefix</cite> 的设置影响网络参数的命名，如果设置为True，则自动给网络参数的名称添加前缀，否则不添加前缀。默认值：True。</p></li>
<li><p><strong>flags</strong> (dict) - Cell的配置信息，目前用于绑定Cell和数据集。用户也通过该参数自定义Cell属性。默认值：None。</p></li>
</ul>
<p><strong>支持平台：</strong></p>
<p><code class="docutils literal notranslate"><span class="pre">Ascend</span></code> <code class="docutils literal notranslate"><span class="pre">GPU</span></code> <code class="docutils literal notranslate"><span class="pre">CPU</span></code></p>
<p><strong>样例：</strong></p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">mindspore.nn</span> <span class="k">as</span> <span class="nn">nn</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">mindspore.ops</span> <span class="k">as</span> <span class="nn">ops</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">class</span> <span class="nc">MyCell</span><span class="p">(</span><span class="n">nn</span><span class="o">.</span><span class="n">Cell</span><span class="p">):</span>
<span class="gp">... </span>    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">forward_net</span><span class="p">):</span>
<span class="gp">... </span>        <span class="nb">super</span><span class="p">(</span><span class="n">MyCell</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="n">auto_prefix</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>
<span class="gp">... </span>        <span class="bp">self</span><span class="o">.</span><span class="n">net</span> <span class="o">=</span> <span class="n">forward_net</span>
<span class="gp">... </span>        <span class="bp">self</span><span class="o">.</span><span class="n">relu</span> <span class="o">=</span> <span class="n">ops</span><span class="o">.</span><span class="n">ReLU</span><span class="p">()</span>
<span class="gp">...</span>
<span class="gp">... </span>    <span class="k">def</span> <span class="nf">construct</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">x</span><span class="p">):</span>
<span class="gp">... </span>        <span class="n">y</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">net</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
<span class="gp">... </span>        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">relu</span><span class="p">(</span><span class="n">y</span><span class="p">)</span>
<span class="go">&gt;&gt;&gt;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">inner_net</span> <span class="o">=</span> <span class="n">nn</span><span class="o">.</span><span class="n">Conv2d</span><span class="p">(</span><span class="mi">120</span><span class="p">,</span> <span class="mi">240</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="n">has_bias</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">weight_init</span><span class="o">=</span><span class="s1">&#39;normal&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">my_net</span> <span class="o">=</span> <span class="n">MyCell</span><span class="p">(</span><span class="n">inner_net</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">my_net</span><span class="o">.</span><span class="n">trainable_params</span><span class="p">())</span>
<span class="gp">... </span><span class="c1"># If the &#39;auto_prefix&#39; set to True or not set when call the &#39;__init__&#39; method of the parent class,</span>
<span class="gp">... </span><span class="c1"># the parameter&#39;s name will be &#39;net.weight&#39;.</span>
<span class="go">[Parameter (name=weight, shape=(240, 120, 4, 4), dtype=Float32, requires_grad=True)]</span>
</pre></div>
</div>
<dl class="method">
<dt id="mindspore.nn.Cell.add_flags">
<code class="sig-name descname">add_flags</code><span class="sig-paren">(</span><em class="sig-param">**flags</em><span class="sig-paren">)</span><a class="headerlink" href="#mindspore.nn.Cell.add_flags" title="Permalink to this definition">¶</a></dt>
<dd><p>为Cell添加自定义属性。</p>
<p>在实例化Cell类时，如果入参flags不为空，会调用此方法。</p>
<p><strong>参数：</strong></p>
<ul class="simple">
<li><p><strong>flags</strong> (dict) - Cell的配置信息，目前用于绑定Cell和数据集。用户也通过该参数自定义Cell属性。默认值：None。</p></li>
</ul>
</dd></dl>

<dl class="method">
<dt id="mindspore.nn.Cell.add_flags_recursive">
<code class="sig-name descname">add_flags_recursive</code><span class="sig-paren">(</span><em class="sig-param">**flags</em><span class="sig-paren">)</span><a class="headerlink" href="#mindspore.nn.Cell.add_flags_recursive" title="Permalink to this definition">¶</a></dt>
<dd><p>如果Cell含有多个子Cell，此方法会递归得给所有子Cell添加自定义属性。</p>
<p><strong>参数：</strong></p>
<ul class="simple">
<li><p><strong>flags</strong> (dict) - Cell的配置信息，目前用于绑定Cell和数据集。用户也通过该参数自定义Cell属性。默认值：None。</p></li>
</ul>
</dd></dl>

<dl class="method">
<dt id="mindspore.nn.Cell.auto_parallel_compile_and_run">
<code class="sig-name descname">auto_parallel_compile_and_run</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#mindspore.nn.Cell.auto_parallel_compile_and_run" title="Permalink to this definition">¶</a></dt>
<dd><p>是否在‘AUTO_PARALLEL’或‘SEMI_AUTO_PARALLEL’模式下执行编译流程。</p>
<p><strong>返回：</strong></p>
<p>bool, <cite>_auto_parallel_compile_and_run</cite> 的值。</p>
</dd></dl>

<dl class="method">
<dt id="mindspore.nn.Cell.bprop_debug">
<em class="property">property </em><code class="sig-name descname">bprop_debug</code><a class="headerlink" href="#mindspore.nn.Cell.bprop_debug" title="Permalink to this definition">¶</a></dt>
<dd><p>在图模式下使用，用于标识是否使用自定义的反向传播函数。</p>
</dd></dl>

<dl class="method">
<dt id="mindspore.nn.Cell.cast_inputs">
<code class="sig-name descname">cast_inputs</code><span class="sig-paren">(</span><em class="sig-param">inputs</em>, <em class="sig-param">dst_type</em><span class="sig-paren">)</span><a class="headerlink" href="#mindspore.nn.Cell.cast_inputs" title="Permalink to this definition">¶</a></dt>
<dd><p>将输入转换为指定类型。</p>
<p><strong>参数：</strong></p>
<ul class="simple">
<li><p><strong>inputs</strong> (tuple[Tensor]) - 输入。</p></li>
<li><p><strong>dst_type</strong> (mindspore.dtype) - 指定的数据类型。</p></li>
</ul>
<p><strong>返回：</strong></p>
<p>tuple[Tensor]类型，转换类型后的结果。</p>
</dd></dl>

<dl class="method">
<dt id="mindspore.nn.Cell.cast_param">
<code class="sig-name descname">cast_param</code><span class="sig-paren">(</span><em class="sig-param">param</em><span class="sig-paren">)</span><a class="headerlink" href="#mindspore.nn.Cell.cast_param" title="Permalink to this definition">¶</a></dt>
<dd><p>在PyNative模式下，根据自动混合精度的精度设置转换Cell中参数的类型。</p>
<p>该接口目前在自动混合精度场景下使用。</p>
<p><strong>参数：</strong></p>
<ul class="simple">
<li><p><strong>param</strong> (Parameter) – 需要被转换类型的输入参数。</p></li>
</ul>
<p><strong>返回：</strong></p>
<p>Parameter类型，转换类型后的参数。</p>
</dd></dl>

<dl class="method">
<dt id="mindspore.nn.Cell.cells">
<code class="sig-name descname">cells</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#mindspore.nn.Cell.cells" title="Permalink to this definition">¶</a></dt>
<dd><p>返回当前Cell的子Cell的迭代器。</p>
<p><strong>返回：</strong></p>
<p>Iteration类型，Cell的子Cell。</p>
</dd></dl>

<dl class="method">
<dt id="mindspore.nn.Cell.cells_and_names">
<code class="sig-name descname">cells_and_names</code><span class="sig-paren">(</span><em class="sig-param">cells=None</em>, <em class="sig-param">name_prefix=&quot;&quot;</em><span class="sig-paren">)</span><a class="headerlink" href="#mindspore.nn.Cell.cells_and_names" title="Permalink to this definition">¶</a></dt>
<dd><p>递归地获取当前Cell及输入 <cite>cells</cite> 的所有子Cell的迭代器，包括Cell的名称及其本身。</p>
<p><strong>参数：</strong></p>
<ul class="simple">
<li><p><strong>cells</strong> (str) – 需要进行迭代的Cell。默认值：None。</p></li>
<li><p><strong>name_prefix</strong> (str) – 作用域。默认值：’’。</p></li>
</ul>
<p><strong>返回：</strong></p>
<p>Iteration类型，当前Cell及输入 <cite>cells</cite> 的所有子Cell和相对应的名称。</p>
<p><strong>样例：</strong></p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">n</span> <span class="o">=</span> <span class="n">Net</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">names</span> <span class="o">=</span> <span class="p">[]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">for</span> <span class="n">m</span> <span class="ow">in</span> <span class="n">n</span><span class="o">.</span><span class="n">cells_and_names</span><span class="p">():</span>
<span class="gp">... </span>    <span class="k">if</span> <span class="n">m</span><span class="p">[</span><span class="mi">0</span><span class="p">]:</span>
<span class="gp">... </span>        <span class="n">names</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">m</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span>
</pre></div>
</div>
</dd></dl>

<dl class="method">
<dt id="mindspore.nn.Cell.check_names">
<code class="sig-name descname">check_names</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#mindspore.nn.Cell.check_names" title="Permalink to this definition">¶</a></dt>
<dd><p>检查Cell中的网络参数名称是否重复。</p>
</dd></dl>

<dl class="method">
<dt id="mindspore.nn.Cell.compile">
<code class="sig-name descname">compile</code><span class="sig-paren">(</span><em class="sig-param">*inputs</em><span class="sig-paren">)</span><a class="headerlink" href="#mindspore.nn.Cell.compile" title="Permalink to this definition">¶</a></dt>
<dd><p>编译Cell为计算图，输入需与construct中定义的输入一致。</p>
<p><strong>参数：</strong></p>
<ul class="simple">
<li><p><strong>inputs</strong> (tuple) – Cell的输入。</p></li>
</ul>
</dd></dl>

<dl class="method">
<dt id="mindspore.nn.Cell.compile_and_run">
<code class="sig-name descname">compile_and_run</code><span class="sig-paren">(</span><em class="sig-param">*inputs</em><span class="sig-paren">)</span><a class="headerlink" href="#mindspore.nn.Cell.compile_and_run" title="Permalink to this definition">¶</a></dt>
<dd><p>编译并运行Cell，输入需与construct中定义的输入一致。</p>
<p><strong>参数：</strong></p>
<ul class="simple">
<li><p><strong>inputs</strong> (tuple) – Cell的输入。</p></li>
</ul>
<p><strong>返回：</strong></p>
<p>Object类型，执行的结果。</p>
</dd></dl>

<dl class="method">
<dt id="mindspore.nn.Cell.construct">
<code class="sig-name descname">construct</code><span class="sig-paren">(</span><em class="sig-param">*inputs</em>, <em class="sig-param">**kwargs</em><span class="sig-paren">)</span><a class="headerlink" href="#mindspore.nn.Cell.construct" title="Permalink to this definition">¶</a></dt>
<dd><p>定义要执行的计算逻辑。所有子类都必须重写此方法。</p>
<p><strong>返回：</strong></p>
<p>Tensor类型，返回计算结果。</p>
</dd></dl>

<dl class="method">
<dt id="mindspore.nn.Cell.exec_checkpoint_graph">
<code class="sig-name descname">exec_checkpoint_graph</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#mindspore.nn.Cell.exec_checkpoint_graph" title="Permalink to this definition">¶</a></dt>
<dd><p>保存checkpoint图。</p>
</dd></dl>

<dl class="method">
<dt id="mindspore.nn.Cell.extend_repr">
<code class="sig-name descname">extend_repr</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#mindspore.nn.Cell.extend_repr" title="Permalink to this definition">¶</a></dt>
<dd><p>在原有描述基础上扩展Cell的描述。</p>
<p>若需要在print时输出个性化的扩展信息，请在您的网络中重新实现此方法。</p>
</dd></dl>

<dl class="method">
<dt id="mindspore.nn.Cell.generate_scope">
<code class="sig-name descname">generate_scope</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#mindspore.nn.Cell.generate_scope" title="Permalink to this definition">¶</a></dt>
<dd><p>为网络中的每个Cell对象生成NameSpace。</p>
</dd></dl>

<dl class="method">
<dt id="mindspore.nn.Cell.get_flags">
<code class="sig-name descname">get_flags</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#mindspore.nn.Cell.get_flags" title="Permalink to this definition">¶</a></dt>
<dd><p>获取该Cell的自定义属性。自定义属性通过 <cite>add_flags</cite> 方法添加。</p>
</dd></dl>

<dl class="method">
<dt id="mindspore.nn.Cell.get_func_graph_proto">
<code class="sig-name descname">get_func_graph_proto</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#mindspore.nn.Cell.get_func_graph_proto" title="Permalink to this definition">¶</a></dt>
<dd><p>返回图的二进制原型。</p>
</dd></dl>

<dl class="method">
<dt id="mindspore.nn.Cell.get_parameters">
<code class="sig-name descname">get_parameters</code><span class="sig-paren">(</span><em class="sig-param">expand=True</em><span class="sig-paren">)</span><a class="headerlink" href="#mindspore.nn.Cell.get_parameters" title="Permalink to this definition">¶</a></dt>
<dd><p>返回Cell中parameter的迭代器。</p>
<p><strong>参数：</strong></p>
<ul class="simple">
<li><p><strong>expand</strong> (bool) – 如果为True，则递归地获取当前Cell和所有子Cell的parameter。否则，只生成当前Cell的子Cell的parameter。默认值：True。</p></li>
</ul>
<p><strong>返回：</strong></p>
<p>Iteration类型，Cell的parameter。</p>
<p><strong>样例：</strong></p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">net</span> <span class="o">=</span> <span class="n">Net</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">parameters</span> <span class="o">=</span> <span class="p">[]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">for</span> <span class="n">item</span> <span class="ow">in</span> <span class="n">net</span><span class="o">.</span><span class="n">get_parameters</span><span class="p">():</span>
<span class="gp">... </span>    <span class="n">parameters</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">item</span><span class="p">)</span>
</pre></div>
</div>
</dd></dl>

<dl class="method">
<dt id="mindspore.nn.Cell.get_scope">
<code class="sig-name descname">get_scope</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#mindspore.nn.Cell.get_scope" title="Permalink to this definition">¶</a></dt>
<dd><p>返回Cell的作用域。</p>
<p><strong>返回：</strong></p>
<p>String类型，网络的作用域。</p>
</dd></dl>

<dl class="method">
<dt id="mindspore.nn.Cell.infer_param_pipeline_stage">
<code class="sig-name descname">infer_param_pipeline_stage</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#mindspore.nn.Cell.infer_param_pipeline_stage" title="Permalink to this definition">¶</a></dt>
<dd><p>推导Cell中当前 <cite>pipeline_stage</cite> 的参数。</p>
<div class="admonition note">
<p class="admonition-title">Note</p>
<ul class="simple">
<li><p>如果某参数不属于任何已被设置 <cite>pipeline_stage</cite> 的Cell，此参数应使用 <cite>add_pipeline_stage</cite> 方法来添加它的 <cite>pipeline_stage</cite> 信息。</p></li>
<li><p>如果某参数P被stageA和stageB两个不同stage的算子使用，那么参数P在使用 <cite>infer_param_pipeline_stage</cite> 之前，应使用 <cite>P.add_pipeline_stage(stageA)</cite> 和 <cite>P.add_pipeline_stage(stageB)</cite> 添加它的stage信息。</p></li>
</ul>
</div>
<p><strong>返回：</strong></p>
<p>属于当前 <cite>pipeline_stage</cite> 的参数。</p>
<p><strong>异常：</strong></p>
<ul class="simple">
<li><p><strong>RuntimeError</strong> – 如果参数不属于任何stage。</p></li>
</ul>
</dd></dl>

<dl class="method">
<dt id="mindspore.nn.Cell.insert_child_to_cell">
<code class="sig-name descname">insert_child_to_cell</code><span class="sig-paren">(</span><em class="sig-param">child_name</em>, <em class="sig-param">child_cell</em><span class="sig-paren">)</span><a class="headerlink" href="#mindspore.nn.Cell.insert_child_to_cell" title="Permalink to this definition">¶</a></dt>
<dd><p>将一个给定名称的子Cell添加到当前Cell。</p>
<p><strong>参数：</strong></p>
<ul class="simple">
<li><p><strong>child_name</strong> (str) – 子Cell名称。</p></li>
<li><p><strong>child_cell</strong> (Cell) – 要插入的子Cell。</p></li>
</ul>
<p><strong>异常：</strong></p>
<ul class="simple">
<li><p><strong>KeyError</strong> – 如果子Cell的名称不正确或与其他子Cell名称重复。</p></li>
<li><p><strong>TypeError</strong> – 如果子Cell的类型不正确。</p></li>
</ul>
</dd></dl>

<dl class="method">
<dt id="mindspore.nn.Cell.insert_param_to_cell">
<code class="sig-name descname">insert_param_to_cell</code><span class="sig-paren">(</span><em class="sig-param">param_name</em>, <em class="sig-param">param</em>, <em class="sig-param">check_name_contain_dot=True</em><span class="sig-paren">)</span><a class="headerlink" href="#mindspore.nn.Cell.insert_param_to_cell" title="Permalink to this definition">¶</a></dt>
<dd><p>向当前Cell添加参数。</p>
<p>将指定名称的参数添加到Cell中。目前在 <cite>mindspore.nn.Cell.__setattr__</cite> 中使用。</p>
<p><strong>参数：</strong></p>
<ul class="simple">
<li><p><strong>param_name</strong> (str) – 参数名称。</p></li>
<li><p><strong>param</strong> (Parameter) – 要插入到Cell的参数。</p></li>
<li><p><strong>check_name_contain_dot</strong> (bool) – 是否对 <cite>param_name</cite> 中的”.”进行检查。默认值：True。</p></li>
</ul>
<p><strong>异常：</strong></p>
<ul class="simple">
<li><p><strong>KeyError</strong> – 如果参数名称为空或包含”.”。</p></li>
<li><p><strong>TypeError</strong> – 如果参数的类型不是Parameter。</p></li>
</ul>
</dd></dl>

<dl class="method">
<dt id="mindspore.nn.Cell.load_parameter_slice">
<code class="sig-name descname">load_parameter_slice</code><span class="sig-paren">(</span><em class="sig-param">params</em><span class="sig-paren">)</span><a class="headerlink" href="#mindspore.nn.Cell.load_parameter_slice" title="Permalink to this definition">¶</a></dt>
<dd><p>根据并行策略获取Tensor分片并替换原始参数。</p>
<p>请参考 <cite>mindspore.common._Executor.compile</cite> 源代码中的用法。</p>
<p><strong>参数：</strong></p>
<p><strong>params</strong> (dict) – 用于初始化数据图的参数字典。</p>
</dd></dl>

<dl class="method">
<dt id="mindspore.nn.Cell.name_cells">
<code class="sig-name descname">name_cells</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#mindspore.nn.Cell.name_cells" title="Permalink to this definition">¶</a></dt>
<dd><p>递归地获取一个Cell中所有子Cell的迭代器。</p>
<p>包括Cell名称和Cell本身。</p>
<p><strong>返回：</strong></p>
<p>Dict[String, Cell]，Cell中的所有子Cell及其名称。</p>
</dd></dl>

<dl class="method">
<dt id="mindspore.nn.Cell.param_prefix">
<em class="property">property </em><code class="sig-name descname">param_prefix</code><a class="headerlink" href="#mindspore.nn.Cell.param_prefix" title="Permalink to this definition">¶</a></dt>
<dd><p>当前Cell的子Cell的参数名前缀。</p>
</dd></dl>

<dl class="method">
<dt id="mindspore.nn.Cell.parameter_layout_dict">
<em class="property">property </em><code class="sig-name descname">parameter_layout_dict</code><a class="headerlink" href="#mindspore.nn.Cell.parameter_layout_dict" title="Permalink to this definition">¶</a></dt>
<dd><p><cite>parameter_layout_dict</cite> 表示一个参数的张量layout，这种张量layout是由分片策略和分布式算子信息推断出来的。</p>
</dd></dl>

<dl class="method">
<dt id="mindspore.nn.Cell.parameters_and_names">
<code class="sig-name descname">parameters_and_names</code><span class="sig-paren">(</span><em class="sig-param">name_prefix=''</em>, <em class="sig-param">expand=True</em><span class="sig-paren">)</span><a class="headerlink" href="#mindspore.nn.Cell.parameters_and_names" title="Permalink to this definition">¶</a></dt>
<dd><p>返回Cell中parameter的迭代器。</p>
<p>包含参数名称和参数本身。</p>
<p><strong>参数：</strong></p>
<ul class="simple">
<li><p><strong>name_prefix</strong> (str): 作用域。默认值： ‘’。</p></li>
<li><p><strong>expand</strong> (bool):  如果为True，则递归地获取当前Cell和所有子Cell的参数及名称；如果为False，只生成当前Cell的子Cell的参数及名称。默认值：True。</p></li>
</ul>
<p><strong>返回：</strong></p>
<p>迭代器，Cell的名称和Cell本身。</p>
<p><strong>样例：</strong></p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">n</span> <span class="o">=</span> <span class="n">Net</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">names</span> <span class="o">=</span> <span class="p">[]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">for</span> <span class="n">m</span> <span class="ow">in</span> <span class="n">n</span><span class="o">.</span><span class="n">parameters_and_names</span><span class="p">():</span>
<span class="gp">... </span>    <span class="k">if</span> <span class="n">m</span><span class="p">[</span><span class="mi">0</span><span class="p">]:</span>
<span class="gp">... </span>        <span class="n">names</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">m</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span>
</pre></div>
</div>
</dd></dl>

<dl class="method">
<dt id="mindspore.nn.Cell.parameters_broadcast_dict">
<code class="sig-name descname">parameters_broadcast_dict</code><span class="sig-paren">(</span><em class="sig-param">recurse=True</em><span class="sig-paren">)</span><a class="headerlink" href="#mindspore.nn.Cell.parameters_broadcast_dict" title="Permalink to this definition">¶</a></dt>
<dd><p>获取这个Cell的参数广播字典。</p>
<p><strong>参数：</strong></p>
<ul class="simple">
<li><p><strong>recurse</strong> (bool): 是否包含子Cell的参数。 默认: True。</p></li>
</ul>
<p><strong>返回：</strong></p>
<p>OrderedDict, 返回参数广播字典。</p>
</dd></dl>

<dl class="method">
<dt id="mindspore.nn.Cell.parameters_dict">
<code class="sig-name descname">parameters_dict</code><span class="sig-paren">(</span><em class="sig-param">recurse=True</em><span class="sig-paren">)</span><a class="headerlink" href="#mindspore.nn.Cell.parameters_dict" title="Permalink to this definition">¶</a></dt>
<dd><p>获取此Cell的parameter字典。</p>
<p><strong>参数：</strong></p>
<ul class="simple">
<li><p><strong>recurse</strong> (bool) – 是否递归得包含所有子Cell的parameter。默认值：True。</p></li>
</ul>
<p><strong>返回：</strong></p>
<p>OrderedDict类型，返回参数字典。</p>
</dd></dl>

<dl class="method">
<dt id="mindspore.nn.Cell.recompute">
<code class="sig-name descname">recompute</code><span class="sig-paren">(</span><em class="sig-param">**kwargs</em><span class="sig-paren">)</span><a class="headerlink" href="#mindspore.nn.Cell.recompute" title="Permalink to this definition">¶</a></dt>
<dd><p>设置Cell重计算。Cell中输出算子以外的所有算子将被设置为重计算。如果一个算子的计算结果被输出到一些反向节点来进行梯度计算，且被设置成重计算，那么我们会在反向传播中重新计算它，而不去存储在前向传播中的中间激活层的计算结果。</p>
<div class="admonition note">
<p class="admonition-title">Note</p>
<ul class="simple">
<li><p>如果计算涉及到诸如随机化或全局变量之类的操作，那么目前还不能保证等价。</p></li>
<li><p>如果该Cell中算子的重计算API也被调用，则该算子的重计算模式以算子的重计算API的设置为准。</p></li>
<li><p>该接口仅配置一次，即当父Cell配置了，子Cell不需再配置。</p></li>
<li><p>Cell的输出算子默认不做重计算，这一点是基于我们减少内存占用的配置经验。如果一个Cell里面只有一个算子而且想要把这个算子设置为重计算的，那么请使用算子的重计算API。</p></li>
<li><p>当应用了重计算且内存充足时，可以配置’mp_comm_recompute=False’来提升性能。</p></li>
<li><p>当应用了重计算但内存不足时，可以配置’parallel_optimizer_comm_recompute=True’来节省内存。有相同融合group的Cell应该配置相同的parallel_optimizer_comm_recompute。</p></li>
</ul>
</div>
<p><strong>参数：</strong></p>
<ul class="simple">
<li><p><strong>mp_comm_recompute</strong> (bool) – 表示在自动并行或半自动并行模式下，指定Cell内部由模型并行引入的通信操作是否重计算。默认值：True。</p></li>
<li><p><strong>parallel_optimizer_comm_recompute</strong> (bool) – 表示在自动并行或半自动并行模式下，指定Cell内部由优化器并行引入的AllGather通信是否重计算。默认值：False。</p></li>
</ul>
</dd></dl>

<dl class="method">
<dt id="mindspore.nn.Cell.register_forward_pre_hook">
<code class="sig-name descname">register_forward_pre_hook</code><span class="sig-paren">(</span><em class="sig-param">hook_fn</em><span class="sig-paren">)</span><a class="headerlink" href="#mindspore.nn.Cell.register_forward_pre_hook" title="Permalink to this definition">¶</a></dt>
<dd><p>设置Cell对象的正向pre_hook函数。此函数仅在PyNative模式下支持。</p>
<div class="admonition note">
<p class="admonition-title">Note</p>
<ul class="simple">
<li><p>hook_fn必须有如下代码定义。 <cite>cell_id</cite> 是已注册Cell对象的信息，包括名称和ID。 <cite>inputs</cite> 是网络正向传播时Cell对象的输入数据。用户可以在hook_fn中打印输入数据或者返回新的输入数据。</p></li>
<li><p>hook_fn返回新的输入数据或者None：hook_fn(cell_id, inputs) -&gt; New inputs or None。</p></li>
<li><p>为了避免脚本在切换到图模式时运行失败，不建议在Cell对象的 <cite>construct</cite> 函数中调用 <cite>register_forward_pre_hook(hook_fn)</cite>。</p></li>
</ul>
</div>
<p><strong>参数：</strong></p>
<ul class="simple">
<li><p><strong>hook_fn</strong> (function) – 捕获Cell对象信息和正向输入数据的hook_fn函数。</p></li>
</ul>
<p><strong>返回：</strong>
- <strong>handle</strong> – 与hook_fn函数对应的handle对象。</p>
<p><strong>异常：</strong>
- <strong>TypeError</strong> – 如果 <cite>hook_fn</cite> 不是Python函数。</p>
</dd></dl>

<dl class="method">
<dt id="mindspore.nn.Cell.register_forward_hook">
<code class="sig-name descname">register_forward_hook</code><span class="sig-paren">(</span><em class="sig-param">hook_fn</em><span class="sig-paren">)</span><a class="headerlink" href="#mindspore.nn.Cell.register_forward_hook" title="Permalink to this definition">¶</a></dt>
<dd><p>设置Cell对象的正向hook函数。此函数仅在PyNative模式下支持。</p>
<div class="admonition note">
<p class="admonition-title">Note</p>
<ul class="simple">
<li><p>hook_fn必须有如下代码定义。 <cite>cell_id</cite> 是已注册Cell对象的信息，包括名称和ID。 <cite>inputs</cite> 是网络正向传播时Cell对象的输入数据。 <cite>outputs</cite> 是网络正向传播时Cell对象的输出数据。用户可以在hook_fn中打印数据或者返回新的输出数据。</p></li>
<li><p>hook_fn返回新的输出数据或者None：hook_fn(cell_id, inputs, outputs) -&gt; New outputs or None。</p></li>
<li><p>为了避免脚本在切换到图模式时运行失败，不建议在Cell对象的 <cite>construct</cite> 函数中调用 <cite>register_forward_hook(hook_fn)</cite>。</p></li>
</ul>
</div>
<p><strong>参数：</strong></p>
<ul class="simple">
<li><p><strong>hook_fn</strong> (function) – 捕获Cell对象信息和正向输入，输出数据的hook_fn函数。</p></li>
</ul>
<p><strong>返回：</strong>
- <strong>handle</strong> – 与hook_fn函数对应的handle对象。</p>
<p><strong>异常：</strong>
- <strong>TypeError</strong> – 如果 <cite>hook_fn</cite> 不是Python函数。</p>
</dd></dl>

<dl class="method">
<dt id="mindspore.nn.Cell.register_backward_hook">
<code class="sig-name descname">register_backward_hook</code><span class="sig-paren">(</span><em class="sig-param">hook_fn</em><span class="sig-paren">)</span><a class="headerlink" href="#mindspore.nn.Cell.register_backward_hook" title="Permalink to this definition">¶</a></dt>
<dd><p>设置Cell对象的反向hook函数。此函数仅在PyNative模式下支持。</p>
<div class="admonition note">
<p class="admonition-title">Note</p>
<ul class="simple">
<li><p>hook_fn必须有如下代码定义。 <cite>cell_id</cite> 是已注册Cell对象的信息，包括名称和ID。 <cite>grad_input</cite> 是反向传递给Cell对象的梯度。 <cite>grad_output</cite> 是Cell对象的反向输出梯度。用户可以在hook_fn中打印梯度数据或者返回新的输出梯度。</p></li>
<li><p>hook_fn返回新的输出梯度或者None：hook_fn(cell_id, grad_input, grad_output) -&gt; New grad_output or None。</p></li>
<li><p>为了避免脚本在切换到图模式时运行失败，不建议在Cell对象的 <cite>construct</cite> 函数中调用 <cite>register_backward_hook(hook_fn)</cite>。</p></li>
</ul>
</div>
<p><strong>参数：</strong></p>
<ul class="simple">
<li><p><strong>hook_fn</strong> (function) – 捕获Cell对象信息和反向输入，输出梯度的hook_fn函数。</p></li>
</ul>
<p><strong>返回：</strong>
- <strong>handle</strong> – 与hook_fn函数对应的handle对象。</p>
<p><strong>异常：</strong>
- <strong>TypeError</strong> – 如果 <cite>hook_fn</cite> 不是Python函数。</p>
</dd></dl>

<dl class="method">
<dt id="mindspore.nn.Cell.remove_redundant_parameters">
<code class="sig-name descname">remove_redundant_parameters</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#mindspore.nn.Cell.remove_redundant_parameters" title="Permalink to this definition">¶</a></dt>
<dd><p>删除冗余参数。</p>
<p>这个接口通常不需要显式调用。</p>
</dd></dl>

<dl class="method">
<dt id="mindspore.nn.Cell.set_auto_parallel">
<code class="sig-name descname">set_auto_parallel</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#mindspore.nn.Cell.set_auto_parallel" title="Permalink to this definition">¶</a></dt>
<dd><p>将Cell设置为自动并行模式。</p>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>如果一个Cell需要使用自动并行或半自动并行模式来进行训练、评估或预测，则该Cell需要调用此接口。</p>
</div>
</dd></dl>

<dl class="method">
<dt id="mindspore.nn.Cell.set_comm_fusion">
<code class="sig-name descname">set_comm_fusion</code><span class="sig-paren">(</span><em class="sig-param">fusion_type</em>, <em class="sig-param">recurse=True</em><span class="sig-paren">)</span><a class="headerlink" href="#mindspore.nn.Cell.set_comm_fusion" title="Permalink to this definition">¶</a></dt>
<dd><p>为Cell中的参数设置融合类型。请参考 <a class="reference internal" href="../mindspore/mindspore.Parameter.html#mindspore.Parameter.comm_fusion" title="mindspore.Parameter.comm_fusion"><code class="xref py py-class docutils literal notranslate"><span class="pre">mindspore.Parameter.comm_fusion</span></code></a> 的描述。</p>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>当函数被多次调用时，此属性值将被重写。</p>
</div>
<p><strong>参数：</strong></p>
<ul class="simple">
<li><p><strong>fusion_type</strong> (int) – Parameter的 <cite>comm_fusion</cite> 属性的设置值。</p></li>
<li><p><strong>recurse</strong> (bool) – 是否递归地设置子Cell的可训练参数。默认值：True。</p></li>
</ul>
</dd></dl>

<dl class="method">
<dt id="mindspore.nn.Cell.set_data_parallel">
<code class="sig-name descname">set_data_parallel</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#mindspore.nn.Cell.set_data_parallel" title="Permalink to this definition">¶</a></dt>
<dd><p>递归设置该Cell中的所有算子的并行策略为数据并行。</p>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>仅在全自动并行(AUTO_PARALLEL)模式下生效。</p>
</div>
<p><strong>样例：</strong></p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">mindspore.nn</span> <span class="k">as</span> <span class="nn">nn</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">net</span> <span class="o">=</span> <span class="n">nn</span><span class="o">.</span><span class="n">Dense</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">net</span><span class="o">.</span><span class="n">set_data_parallel</span><span class="p">()</span>
</pre></div>
</div>
</dd></dl>

<dl class="method">
<dt id="mindspore.nn.Cell.shard">
<code class="sig-name descname">shard</code><span class="sig-paren">(</span><em class="sig-param">in_axes</em>, <em class="sig-param">out_axes</em>, <em class="sig-param">device=&quot;Ascend&quot;</em>, <em class="sig-param">level=0</em><span class="sig-paren">)</span><a class="headerlink" href="#mindspore.nn.Cell.shard" title="Permalink to this definition">¶</a></dt>
<dd><p>指定输入/输出Tensor的分布策略，其余算子的策略推导得到。在PyNative模式下，可以利用此方法指定某个Cell以图模式进行分布式执行。 in_axes/out_axes需要为元组类型，
其中的每一个元素指定对应的输入/输出的Tensor分布策略，可参考： <cite>mindspore.ops.Primitive.shard</cite> 的描述，也可以设置为None，会默认以数据并行执行。
其余算子的并行策略由输入输出指定的策略推导得到。</p>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>需设置为PyNative模式，并且全自动并行(AUTO_PARALLEL)，同时设置`set_auto_parallel_context`中的搜索模式(search mode)为”sharding_propagation”，或半自动并行（SEMI_AUTO_PARALLEL)。</p>
</div>
<p><strong>参数：</strong></p>
<ul class="simple">
<li><p><strong>in_axes</strong> (tuple) – 指定各输入的切分策略，输入元组的每个元素可以为元组或None，元组即具体指定输入每一维的切分策略，None则会默认以数据并行执行。</p></li>
<li><p><strong>out_axes</strong> (tuple) – 指定各输出的切分策略，用法同in_axes。</p></li>
<li><p><strong>device</strong> (string) - 指定执行设备，可以为[“CPU”, “GPU”, “Ascend”]中任意一个，默认值：”Ascend”。目前尚未使能。</p></li>
<li><p><strong>level</strong> (int) - 指定搜索切分策略的目标函数，即是最大化计算通信比、最小化内存消耗、最大化执行速度等。可以为[0, 1, 2]中任意一个，默认值：0。目前仅支持
最大化计算通信比，其余模式尚未使能。</p></li>
</ul>
<p><strong>返回：</strong></p>
<p>Cell类型，Cell本身。</p>
</dd></dl>

<dl class="method">
<dt id="mindspore.nn.Cell.set_grad">
<code class="sig-name descname">set_grad</code><span class="sig-paren">(</span><em class="sig-param">requires_grad=True</em><span class="sig-paren">)</span><a class="headerlink" href="#mindspore.nn.Cell.set_grad" title="Permalink to this definition">¶</a></dt>
<dd><p>Cell的梯度设置。在PyNative模式下，该参数指定Cell是否需要梯度。如果为True，则在执行正向网络时，将生成需要计算梯度的反向网络。</p>
<p><strong>参数：</strong></p>
<ul class="simple">
<li><p><strong>requires_grad</strong> (bool) – 指定网络是否需要梯度，如果为True，PyNative模式下Cell将构建反向网络。默认值：True。</p></li>
</ul>
<p><strong>返回：</strong></p>
<p>Cell类型，Cell本身。</p>
</dd></dl>

<dl class="method">
<dt id="mindspore.nn.Cell.set_parallel_input_with_inputs">
<code class="sig-name descname">set_parallel_input_with_inputs</code><span class="sig-paren">(</span><em class="sig-param">*inputs</em><span class="sig-paren">)</span><a class="headerlink" href="#mindspore.nn.Cell.set_parallel_input_with_inputs" title="Permalink to this definition">¶</a></dt>
<dd><p>通过并行策略对输入张量进行切分。</p>
<p><strong>参数：</strong></p>
<p><strong>inputs</strong> (tuple) – construct方法的输入。</p>
</dd></dl>

<dl class="method">
<dt id="mindspore.nn.Cell.set_param_fl">
<code class="sig-name descname">set_param_fl</code><span class="sig-paren">(</span><em class="sig-param">push_to_server=False</em>, <em class="sig-param">pull_from_server=False</em>, <em class="sig-param">requires_aggr=True</em><span class="sig-paren">)</span><a class="headerlink" href="#mindspore.nn.Cell.set_param_fl" title="Permalink to this definition">¶</a></dt>
<dd><p>设置参数与服务器交互的方式。</p>
<p><strong>参数：</strong></p>
<ul class="simple">
<li><p><strong>push_to_server</strong> (bool) – 是否将参数推送到服务器。默认值：False。</p></li>
<li><p><strong>pull_from_server</strong> (bool) – 是否从服务器提取参数。默认值：False。</p></li>
<li><p><strong>requires_aggr</strong> (bool) – 是否在服务器中聚合参数。默认值：True。</p></li>
</ul>
</dd></dl>

<dl class="method">
<dt id="mindspore.nn.Cell.set_param_ps">
<code class="sig-name descname">set_param_ps</code><span class="sig-paren">(</span><em class="sig-param">recurse=True</em>, <em class="sig-param">init_in_server=False</em><span class="sig-paren">)</span><a class="headerlink" href="#mindspore.nn.Cell.set_param_ps" title="Permalink to this definition">¶</a></dt>
<dd><p>设置可训练参数是否由参数服务器更新，以及是否在服务器上初始化可训练参数。</p>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>只在运行的任务处于参数服务器模式时有效。</p>
</div>
<p><strong>参数：</strong></p>
<ul class="simple">
<li><p><strong>recurse</strong> (bool) – 是否设置子网络的可训练参数。默认值：True。</p></li>
<li><p><strong>init_in_server</strong> (bool) – 是否在服务器上初始化由参数服务器更新的可训练参数。默认值：False。</p></li>
</ul>
</dd></dl>

<dl class="method">
<dt id="mindspore.nn.Cell.set_train">
<code class="sig-name descname">set_train</code><span class="sig-paren">(</span><em class="sig-param">mode=True</em><span class="sig-paren">)</span><a class="headerlink" href="#mindspore.nn.Cell.set_train" title="Permalink to this definition">¶</a></dt>
<dd><p>将Cell设置为训练模式。</p>
<p>设置当前Cell和所有子Cell的训练模式。对于训练和预测具有不同结构的网络层(如 <cite>BatchNorm</cite>)，将通过这个属性区分分支。如果设置为True，则执行训练分支，否则执行另一个分支。</p>
<p><strong>参数：</strong></p>
<ul class="simple">
<li><p><strong>mode</strong> (bool) – 指定模型是否为训练模式。默认值：True。</p></li>
</ul>
<p><strong>返回：</strong></p>
<p>Cell类型，Cell本身。</p>
</dd></dl>

<dl class="method">
<dt id="mindspore.nn.Cell.to_float">
<code class="sig-name descname">to_float</code><span class="sig-paren">(</span><em class="sig-param">dst_type</em><span class="sig-paren">)</span><a class="headerlink" href="#mindspore.nn.Cell.to_float" title="Permalink to this definition">¶</a></dt>
<dd><p>在Cell和所有子Cell的输入上添加类型转换，以使用特定的浮点类型运行。</p>
<p>如果 <cite>dst_type</cite> 是 <cite>mindspore.dtype.float16</cite> ，Cell的所有输入(包括作为常量的input， Parameter， Tensor)都会被转换为float16。请参考 <cite>mindspore.build_train_network</cite> 的源代码中的用法。</p>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>多次调用将产生覆盖。</p>
</div>
<p><strong>参数：</strong></p>
<ul class="simple">
<li><p><strong>dst_type</strong> (mindspore.dtype) – Cell转换为 <cite>dst_type</cite> 类型运行。 <cite>dst_type</cite> 可以是 <cite>mindspore.dtype.float16</cite> 或者  <cite>mindspore.dtype.float32</cite> 。</p></li>
</ul>
<p><strong>返回：</strong></p>
<p>Cell类型，Cell本身。</p>
<p><strong>异常：</strong></p>
<ul class="simple">
<li><p><strong>ValueError</strong> – 如果 <cite>dst_type</cite> 不是 <cite>mindspore.dtype.float32</cite> ，也不是 <cite>mindspore.dtype.float16</cite>。</p></li>
</ul>
<p><strong>样例：</strong></p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">mindspore.nn</span> <span class="k">as</span> <span class="nn">nn</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">mindspore</span> <span class="kn">import</span> <span class="n">dtype</span> <span class="k">as</span> <span class="n">mstype</span>
<span class="go">&gt;&gt;&gt;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">net</span> <span class="o">=</span> <span class="n">nn</span><span class="o">.</span><span class="n">Conv2d</span><span class="p">(</span><span class="mi">120</span><span class="p">,</span> <span class="mi">240</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="n">has_bias</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">weight_init</span><span class="o">=</span><span class="s1">&#39;normal&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">net</span><span class="o">.</span><span class="n">to_float</span><span class="p">(</span><span class="n">mstype</span><span class="o">.</span><span class="n">float16</span><span class="p">)</span>
</pre></div>
</div>
</dd></dl>

<dl class="method">
<dt id="mindspore.nn.Cell.trainable_params">
<code class="sig-name descname">trainable_params</code><span class="sig-paren">(</span><em class="sig-param">recurse=True</em><span class="sig-paren">)</span><a class="headerlink" href="#mindspore.nn.Cell.trainable_params" title="Permalink to this definition">¶</a></dt>
<dd><p>返回Cell的可训练参数。</p>
<p>返回一个可训练参数的列表。</p>
<p><strong>参数：</strong></p>
<ul class="simple">
<li><p><strong>recurse</strong> (bool) – 是否递归地包含当前Cell的所有子Cell的可训练参数。默认值：True。</p></li>
</ul>
<p><strong>返回：</strong></p>
<p>List类型，可训练参数列表。</p>
</dd></dl>

<dl class="method">
<dt id="mindspore.nn.Cell.untrainable_params">
<code class="sig-name descname">untrainable_params</code><span class="sig-paren">(</span><em class="sig-param">recurse=True</em><span class="sig-paren">)</span><a class="headerlink" href="#mindspore.nn.Cell.untrainable_params" title="Permalink to this definition">¶</a></dt>
<dd><p>返回Cell的不可训练参数。</p>
<p>返回一个不可训练参数的列表。</p>
<p><strong>参数：</strong></p>
<ul class="simple">
<li><p><strong>recurse</strong> (bool) – 是否递归地包含当前Cell的所有子Cell的不可训练参数。默认值：True。</p></li>
</ul>
<p><strong>返回：</strong></p>
<p>List类型，不可训练参数列表。</p>
</dd></dl>

<dl class="method">
<dt id="mindspore.nn.Cell.update_cell_prefix">
<code class="sig-name descname">update_cell_prefix</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#mindspore.nn.Cell.update_cell_prefix" title="Permalink to this definition">¶</a></dt>
<dd><p>递归地更新所有子Cell的 <cite>param_prefix</cite> 。</p>
<p>在调用此方法后，可以通过Cell的 <cite>param_prefix</cite> 属性获取该Cell的所有子Cell的名称前缀。</p>
</dd></dl>

<dl class="method">
<dt id="mindspore.nn.Cell.update_cell_type">
<code class="sig-name descname">update_cell_type</code><span class="sig-paren">(</span><em class="sig-param">cell_type</em><span class="sig-paren">)</span><a class="headerlink" href="#mindspore.nn.Cell.update_cell_type" title="Permalink to this definition">¶</a></dt>
<dd><p>量化感知训练网络场景下，更新当前Cell的类型。</p>
<p>此方法将Cell类型设置为 <cite>cell_type</cite> 。</p>
<p><strong>参数：</strong></p>
<ul class="simple">
<li><p><strong>cell_type</strong> (str) – 被更新的类型，<cite>cell_type</cite> 可以是”quant”或”second-order”。</p></li>
</ul>
</dd></dl>

<dl class="method">
<dt id="mindspore.nn.Cell.update_parameters_name">
<code class="sig-name descname">update_parameters_name</code><span class="sig-paren">(</span><em class="sig-param">prefix=&quot;&quot;</em>, <em class="sig-param">recurse=True</em><span class="sig-paren">)</span><a class="headerlink" href="#mindspore.nn.Cell.update_parameters_name" title="Permalink to this definition">¶</a></dt>
<dd><p>给网络参数名称添加 <cite>prefix</cite> 前缀字符串。</p>
<p><strong>参数：</strong></p>
<ul class="simple">
<li><p><strong>prefix</strong> (str) – 前缀字符串。默认值：’’。</p></li>
<li><p><strong>recurse</strong> (bool) – 是否递归地包含所有子Cell的参数。默认值：True。</p></li>
</ul>
</dd></dl>

</dd></dl>

</div>


           </div>
           
          </div>
          <footer>
    <div class="rst-footer-buttons" role="navigation" aria-label="footer navigation">
        <a href="mindspore.nn.GraphCell.html" class="btn btn-neutral float-right" title="mindspore.nn.GraphCell" accesskey="n" rel="next">Next <span class="fa fa-arrow-circle-right" aria-hidden="true"></span></a>
        <a href="../mindspore.nn.html" class="btn btn-neutral float-left" title="mindspore.nn" accesskey="p" rel="prev"><span class="fa fa-arrow-circle-left" aria-hidden="true"></span> Previous</a>
    </div>

  <hr/>

  <div role="contentinfo">
    <p>
        &#169; Copyright 2021, MindSpore.

    </p>
  </div>
    
    
    
    Built with <a href="https://www.sphinx-doc.org/">Sphinx</a> using a
    
    <a href="https://github.com/readthedocs/sphinx_rtd_theme">theme</a>
    
    provided by <a href="https://readthedocs.org">Read the Docs</a>. 

</footer>
        </div>
      </div>

    </section>

  </div>
  

  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script>

  
  
    
   

</body>
</html>